package com.yaorange.certificate.service.impl;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.yaorange.certificate.domain.NjtUserPerfect;
import com.yaorange.certificate.domain.NjtUserRegister;
import com.yaorange.certificate.service.INjtUserPerfectService;
import com.yaorange.certificate.service.INjtUserRegisterService;
import com.yaorange.certificate.service.NjtCommonService;
import com.yaorange.certificate.utils.QiniuUtils;
import com.yaorange.certificate.vo.UserInfoVo;
import com.yaorange.common.core.domain.entity.SysUser;
import com.yaorange.common.core.domain.model.LoginUser;
import com.yaorange.common.exception.ServiceException;
import com.yaorange.common.utils.SecurityUtils;
import com.yaorange.common.utils.uuid.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author coach tam
 * @email 327395128@qq.com
 * @values 坚持灵活 灵活坚持
 * @since 2025/5/6
 */
@Service
public class NjtCommonServiceImpl implements NjtCommonService {
    @Autowired
    private INjtUserRegisterService  njtUserRegisterService;
    @Autowired
    private INjtUserPerfectService njtUserPerfectService;
    @Override
    @Transactional(readOnly = true)
    public UserInfoVo getLoginUser() {
        UserInfoVo userInfoVo = new UserInfoVo();
        //此处有三种方式可以拿到用户信息：1，当前线程中（当前进程的堆中）获取，2，从redis缓存中获取，3，从数据库中获取
        LoginUser loginUser = SecurityUtils.getLoginUser();
        userInfoVo.setSysUser(loginUser.getUser());
        //用户注册信息
        NjtUserRegister njtUserRegister = njtUserRegisterService.selectNjtUserRegisterById(loginUser.getUser().getUserId());
        if(njtUserRegister!=null){
            userInfoVo.setUserRegister(njtUserRegister);
        }
        //用户完善信息
        NjtUserPerfect njtUserPerfect = njtUserPerfectService.selectNjtUserPerfectById(loginUser.getUser().getUserId());
        if(njtUserPerfect!=null){
            userInfoVo.setUserPerfect(njtUserPerfect);
        }
        return userInfoVo;
    }

/*    @Override
    public String upload(MultipartFile file) {
        //保存到本地硬盘
        String originalFilename = file.getOriginalFilename();
        try {
            //保证文件名的唯一性（uuid）
            originalFilename = UUID.fastUUID() + "_" + originalFilename;
            file.transferTo(new File("D:\\njt\\static\\wechat\\" + originalFilename));
        } catch (IOException e) {
            throw new ServiceException("保存文件到本地异常:" + e.getMessage());
        }
        return "http://localhost:8080/wechat/"+originalFilename;
    }*/

    @Override
    public String upload(MultipartFile file) {
        //保存到本地硬盘
        String originalFilename = file.getOriginalFilename();
        try {
            //保证文件名的唯一性（uuid）
            originalFilename = UUID.fastUUID() + "_" + originalFilename;
            QiniuUtils.upload2Qiniu(file.getBytes(), originalFilename);
        } catch (Exception e) {
            throw new ServiceException("保存文件到七牛云异常:" + e.getMessage());
        }
        return "http://svtluqwe2.hn-bkt.clouddn.com/"+originalFilename;
    }
}
